export function formatDate(date, fmt) {
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  const o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  }
  for (const k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      const str = o[k] + ''
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str))
    }
  }
  return fmt
}

function padLeftZero(str) {
  return ('00' + str).substr(str.length)
}
export function ObjToQuery(obj) {
  let str = ''
  for (const keys in obj) {
    if (obj[keys] !== null && obj[keys] !== 'null' && obj[keys] !== 'undefined' && obj[keys] !== undefined && obj[keys] !== '') {
      str += keys + '=' + obj[keys] + '&'
    }
  }
  return str
}
export const ObjToQueryString = function(obj) {
  let str = ''
  for (const keys in obj) {
    if (obj[keys] !== null && obj[keys] !== 'null' && obj[keys] !== 'undefined' && obj[keys] !== undefined && obj[keys] !== '') {
      str += keys + '=' + obj[keys] + '&'
    }
  }
  return str
}
export const mul = function(a, b) {
  let c = 0
  const d = a.toString()
  const e = b.toString()
  try {
    c += d.split('.')[1].length
  } catch (f) {
    c = 0
  }
  try {
    c += e.split('.')[1].length
  } catch (f) {
    c = 0
  }
  return Number(d.replace('.', '')) * Number(e.replace('.', '')) / Math.pow(10, c)
}
export const sub = function(a, b) {
  let c, d
  try {
    c = a.toString().split('.')[1].length
  } catch (f) {
    c = 0
  }
  try {
    d = b.toString().split('.')[1].length
  } catch (f) {
    d = 0
  }
  const e = Math.pow(10, Math.max(c, d))
  return (mul(a, e) - mul(b, e)) / e
}
export const div = function(a, b) {
  let e = 0
  let f = 0
  try {
    e = a.toString().split('.')[1].length
  } catch (g) {
    e = 0
  }
  try {
    f = b.toString().split('.')[1].length
  } catch (g) {
    f = 0
  }
  const c = Number(a.toString().replace('.', ''))
  const d = Number(b.toString().replace('.', ''))
  return mul(c / d, Math.pow(10, f - e))
}
